//
//  WelcomeViewController.swift
//  WeiBlog
//
//  Created by apple on 15/10/19.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomeViewController: UIViewController {

    // MARK:-------视图周期相关
    override func loadView() {
        view = imageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setUI()
        
        iconImageView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccountViewModel.avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        
        welcomeLabel.text = UserAccountViewModel.sharedUserAccountViewModel.userAccountInfo?.screen_name

    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        self.iconImageView.snp_updateConstraints(closure: { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-view.bounds.height*0.7)

        })
        
        welcomeLabel.alpha = 0
        ///  动画
        ///
        ///  - parameter <Tduration:             动画时间
        ///  - parameter delay:                  延迟时间
        ///  - parameter usingSpringWithDamping: 弹力系数(越小越弹)
        ///  - parameter initialSpringVelocity:  重力加速度
        ///  - parameter options:                动画可选(不知道是什么)
        ///  - parameter animations:             动画(可动画属性)
        ///  - parameter completion:             完成
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            // MARK:------- 自动布局的动画
            //升级(已有)约束
            self.view.layoutIfNeeded()
            
            }) { (isFInished) -> Void in
                
                //头像动画结束之后显示文字
                UIView.animateWithDuration(0.2, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (isFinished) -> Void in
                        //跳转主页面
                        
                        ///  延时操作
                     
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * NSEC_PER_SEC)),dispatch_get_main_queue(), { () -> Void in
                            
                            // MARK:-------通知
                            NSNotificationCenter.defaultCenter().postNotificationName("WBJumpToNotification", object: nil)
                        })
                })
        }
    }

    // MARK:-------懒加载
    /// 主背景图片
    private lazy var imageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    /// 头像
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 欢迎文字
    private lazy var welcomeLabel:UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
//        label.sizeToFit()
        return label
        }()

}


// MARK:-------设置界面
extension WelcomeViewController
{
    private func setUI(){
        
        //添加控件
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)

        iconImageView.layer.cornerRadius = 45
        iconImageView.layer.masksToBounds = true
        //自动布局

        ///  头像
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(90)
            make.height.equalTo(90)
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-view.bounds.height*0.4)
        }
        ///  欢迎文字
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.iconImageView.snp_centerX)
            make.top.equalTo(self.iconImageView.snp_bottom).offset(16)
        }
        
    }
}